var util = require('util');
var model = require('../modules/mongoSchemes').tagModel;
//var todoModel = require('../modules/mongoSchemes').todoModel;

var add = function(data,callback) {	
	var newTag = new model();
	newTag.title = data.title;
    if(data.color && data.color != ""){
        newTag.color = data.color;
    }
    if(data.class_name && data.class_name != ""){
        newTag.class_name = data.class_name;
    }
	newTag.save(function(err){
		if(err){
			util.log("Fatal "+err);
			callback(err);
		}else{
			callback(null, newTag);
		}
	});
};

var find = function(condition, callback) {
    model.find(condition).sort({'order':-1}).sort({'last_modified_date':-1}).exec(callback);;
}

var findAll = function(callback) {
    find({}, callback);
}

var findByTitle = function(title, callback){
    model.findOne({title: title},function(err, doc){
        if (err) {
            util.log('Fatal '+ err);
            callback(err, null);
        }
        callback(null, doc);
    });
};

var findById = function(id, callback){
    model.findOne({_id: id}, function(err, doc){
        if (err) {
            util.log('Fatal '+ err);
            callback(err, null);
        }
        callback(null, doc);
    });
};

var findExistByTitle = function(id, title, callback){
    model.find({title: title},function(err, docs){
        if (err) {
            util.log('Fatal '+ err);
            callback(err, null);
        }
        var doc = null;
        console.log(docs)
        if(docs != null && docs.length > 0){
            for (var i = docs.length - 1; i >= 0; i--) {
                if(docs[i]._id != id){
                    doc = docs[i];
                    break;
                }
            };
        }
        callback(null, doc);
    });
};

var remove = function(id, callback) {
    findById(id, function(err, doc) {
        if (err)
            callback(err);
        else {
            util.log(util.inspect(doc));
            doc.remove();
            callback(null);
        }
    });
};

var editTitle = function(data, callback) {
    findById(data._id, function(err, doc) {
        if (err)
            callback(err);
        else {
            doc.last_modified_date = new Date();
            doc.title = data.title;
            doc.color = data.color;
            doc.class_name = data.class_name;
            doc.save(function(err, tagEntity) {
                if (err) {
                    util.log('Fatal '+ err);
                    callback(err);
                } 
                callback(null, tagEntity);
/*
                else{
                    todoModel.where("tags._id").equals(doc._id).exec(function(err, docs){                        
                        for (var i = docs.length - 1; i >= 0; i--) {
                            var todo = docs[i];
                            var tag = todo.tags.id(data._id)
                            tag.title = data.title;
                            tag.class_name = data.class_name;
                            console.log(doc[i])
                            todo.save();
                        };
                        callback(null);
                    });
                }
                */
            });
        }
    });
};

var search = function(data, callback){
    var condition = {};
    if(data.keyword && data.keyword.trim() != "")
    {
        condition["$or"] = [
            {"title": new RegExp(data.keyword.trim())}, 
            {"class_name": new RegExp(data.keyword)}
        ];
    }
    else if(data.title && data.title.trim() != "")
    {
        condition.title = new RegExp(data.title.trim());        
    }
    if(data.class_name && data.class_name.trim() != "")
    {
        condition.class_name = new RegExp(data.class_name.trim());        
    }

    //console.log(condition)
    find(condition, callback);
}

exports.add = add;
exports.find = find;
exports.findAll = findAll;
exports.findByTitle = findByTitle;
exports.findExistByTitle = findExistByTitle;
exports.findById = findById;
exports.remove = remove;
exports.editTitle = editTitle;
exports.search = search;

